// src/store/slice/search.ts
import { createEntityAdapter, createSlice, Draft, EntityState, PayloadAction } from "@reduxjs/toolkit";
import { AppState } from "@store/index";

// 创建搜索结果状态实体适配器
const searchEntityAdaptor = createEntityAdapter({
  selectId: (news:any) => news.art_id,
});

// 创建用于管理搜索结果的状态切片
const { reducer: searchReducer, actions } = createSlice({
  name: "search",
  initialState: searchEntityAdaptor.getInitialState(),
  reducers: {
    // 保存搜索结果
    saveResults: searchEntityAdaptor.addMany,
    // 清空搜索结果
    clearResults: searchEntityAdaptor.removeAll,
  },
});

// 导出搜索结果状态选择器
export const searchResultSelectors =
searchEntityAdaptor.getSelectors<AppState>(
    (state) => state.searchReducer
  );

export const { saveResults, clearResults } = actions;
export default searchReducer;